<?php

namespace YCommerce\GlobalBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use YCommerce\GlobalBundle\Entity\Produit;

class DefaultController extends Controller
{
    public function helloAction($name)
    {
        return $this->render('YCommerceGlobalBundle:Default:index.html.twig', array('name' => $name));
    }
	
    public function indexAction()
    {
        $em = $this->container->get('doctrine')->getManager();
                
        $lastProducts = $em->getRepository('YCommerceGlobalBundle:Produit')->getLastProducts();       
        $tendanceProducts = $em->getRepository('YCommerceGlobalBundle:Produit')->getTendanceProducts();
        
        return $this->render('YCommerceGlobalBundle:Default:index.html.twig', array(
            'lastProducts' => $lastProducts,
            'tendanceProducts' => $tendanceProducts,
            'activeMenuItem' => 1
        ));
    }
    
    public function produitsAction()
    {
        $em = $this->container->get('doctrine')->getManager();          
        $tendanceProducts = $em->getRepository('YCommerceGlobalBundle:Produit')->getTendanceProducts();
        
        $user = $this->container->get('security.context')->getToken()->getUser();
        $products = array();
        if($user != 'anon.' && $user->hasRole('ROLE_USER')) {
            $products = $em->getRepository('YCommerceGlobalBundle:Produit')->getCustomizedProducts($user);       
        } else {
            $products = $em->getRepository('YCommerceGlobalBundle:Produit')->findAll();       
        }
        
        return $this->render('YCommerceGlobalBundle:Default:produits.html.twig', array(
            'tendanceProducts' => $tendanceProducts,
            'products' => $products,
            'activeMenuItem' => 2
        ));
    }
    
    public function produitAction(Produit $p)
    {
        $em = $this->container->get('doctrine')->getManager();  
        
        $p->addSimpleHit();
        $em->persist($p);
        $em->flush();
        
        return $this->render('YCommerceGlobalBundle:Default:produit.html.twig', array(
            'produit' => $p,
            'activeMenuItem' => 2
        ));
    }
    
    public function cartAction()
    {
        $em = $this->container->get('doctrine')->getManager();         
        $allIds = $this->container->get('session')->get('cart');
        $allIds = explode(',', $allIds);
        
        $products = array();
        $total = 0;
        foreach($allIds as $id) {
            if(strlen($id) > 0) {
                $p = $em->getRepository('YCommerceGlobalBundle:Produit')->find($id);
                $products[] = $p;
                $total += $p->getPrix();
            }
        }
        
        return $this->render('YCommerceGlobalBundle:Default:cart.html.twig', array(
            'products' => $products,
            'total' => $total,
            'activeMenuItem' => 5
        ));
    }
    
    public function addtocartAction(Produit $p)
    {
        $previous = $this->container->get('session')->get('cart');
        if(!$previous) {
            $previous = $p->getId();
        } else {
            $previous .= ','.$p->getId();
        }
        
        $this->container->get('session')->set('cart',$previous);
        $this->get('session')->getFlashBag()->add('info', 'Le produit "'.$p->getName().'" a bien été ajouté dans votre panier !');
        
        return $this->render('YCommerceGlobalBundle:Default:produit.html.twig', array(
            'produit' => $p,
            'activeMenuItem' => 2
        ));
    }
    
    public function cartresetAction() 
    {
        $this->container->get('session')->remove('cart');
        $this->get('session')->getFlashBag()->add('info', 'Panier vidé');
        
        return $this->render('YCommerceGlobalBundle:Default:cart.html.twig', array(
            'products' => array(),
            'total' => 0,
            'activeMenuItem' => 5
        ));
    }
}
